<template>
  <div class="app-container">
    <el-tabs v-model="businessType" type="card" @tab-click="handleClick">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:platformRights:add']"
          >新增</el-button>
        </el-col>
      </el-row>
      <el-tab-pane label="使用时长" name="1">
        <el-table v-loading="loading" :data="platformRightsList">
          <el-table-column :formatter="defaultTableColumnDate" label="名称" align="center" prop="rightName" />
          <el-table-column :formatter="defaultTableColumnDate" label="天数" align="center" prop="amount" />
          <el-table-column :formatter="defaultTableColumnDate" label="售价" align="center" prop="price" />
          <el-table-column :formatter="defaultTableColumnDate" label="赠送合同份数" align="center" prop="giftContract" />
          <el-table-column :formatter="defaultTableColumnDate" label="赠送短信数量" align="center" prop="giftSms" />
          <el-table-column :formatter="defaultTableColumnDate" label="排序值" align="center" prop="remark" />
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformRights:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:platformRights:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="合同份数" name="2">
        <el-table v-loading="loading" :data="platformRightsList">
          <el-table-column :formatter="defaultTableColumnDate" label="名称" align="center" prop="rightName" />
          <el-table-column :formatter="defaultTableColumnDate" label="份数" align="center" prop="amount" />
          <el-table-column :formatter="defaultTableColumnDate" label="售价" align="center" prop="price" />
          <el-table-column :formatter="defaultTableColumnDate" label="排序值" align="center" prop="remark" />
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformRights:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:platformRights:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="短信条数" name="3">
        <el-table v-loading="loading" :data="platformRightsList">
          <el-table-column :formatter="defaultTableColumnDate" label="名称" align="center" prop="rightName" />
          <el-table-column :formatter="defaultTableColumnDate" label="条数" align="center" prop="amount" />
          <el-table-column :formatter="defaultTableColumnDate" label="售价" align="center" prop="price" />
          <el-table-column :formatter="defaultTableColumnDate" label="排序值" align="center" prop="remark" />
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformRights:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:platformRights:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台权益对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="名称" prop="rightName">
          <el-input v-model="form.rightName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item :label="amountName[businessType]" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="赠送合同份数" prop="giftContract" v-if="businessType=='1'">
          <el-input v-model="form.giftContract" placeholder="请输入赠送合同份数" />
        </el-form-item>
        <el-form-item label="赠送短信数量" prop="giftSms" v-if="businessType=='1'">
          <el-input v-model="form.giftSms" placeholder="请输入赠送短信数量" />
        </el-form-item>
        <el-form-item label="排序值" prop="remark">
          <el-input-number style="text-align: left;width: 100%;" :min="1" :controls="false" :precision="0" v-model="form.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlatformRights, getPlatformRights, delPlatformRights, addPlatformRights, updatePlatformRights } from "@/api/system/platformRights";

export default {
  name: "PlatformRights",
  data() {
    return {
      amountName: {
        '1': '天数',
        '2': '份数',
        '3': '条数',
      },
      businessType: '1',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台权益表格数据
      platformRightsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rightName: null,
        businessType: null,
        amount: null,
        price: null,
        giftContract: null,
        giftSms: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rightName: [
          { required: true, trigger: "blur", message: "请输入名称" }
        ],
        amount: [
          { required: true, trigger: "blur", message: "请输入" }
        ],
        price: [
          { required: true, trigger: "blur", message: "请输入售价" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询平台权益列表 */
    getList() {
      this.loading = true;
      listPlatformRights({...this.queryParams, businessType: this.businessType}).then(response => {
        this.platformRightsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rightName: null,
        businessType: null,
        amount: null,
        price: null,
        giftContract: null,
        giftSms: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台权益";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlatformRights(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台权益";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlatformRights(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlatformRights({...this.form, businessType: this.businessType}).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除平台权益编号为"' + ids + '"的数据项？').then(function() {
        return delPlatformRights(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/platformRights/export', {
        ...this.queryParams
      }, `platformRights_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
